package com.gongsir.controller;

import com.github.pagehelper.PageInfo;
import com.gongsir.entity.Movie;
import com.gongsir.service.MovieService;
import com.gongsir.utils.ResponseData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author 码之泪殇
 * @date 2020/8/24 10:46
 * 到达胜利之前，无法回头
 * 电影管理类
 */
@Controller
@Slf4j
@RequestMapping("/movie")
public class MovieController {

    @Autowired
    private MovieService movieService;

    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 查询电影信息, 支持分页和模糊搜索
     * @param page 页码
     * @param limit 限制
     * @param keywords 电影名称或者演员名称关键字
     * @param orderBy 排序
     * @return ResponseData-Json格式
     */
    @GetMapping("/movies")
    @ResponseBody
    public ResponseData getAllMovies(@RequestParam(defaultValue = "1") int page,
                                     @RequestParam(defaultValue = "10") int limit,
                                     @RequestParam(value = "keywords", required = false,defaultValue = "") String keywords,
                                     @RequestParam(required = false,defaultValue = "id desc") String orderBy) {
        log.info("参数:page =  {},limit = {},keywords  = {},orderBy = {}",page,limit,keywords,orderBy);
        // 得到分页信息
        PageInfo<Movie> pageInfo = movieService.getAllMovie(keywords,page,limit,orderBy);
        List<Movie> movieList = pageInfo.getList();
        ResponseData responseData = new ResponseData();
        if (!movieList.isEmpty()) {
            responseData = ResponseData.ok().putDataValue("status",true)
                    .putDataValue("data",movieList);
            responseData.setCount(pageInfo.getTotal());
        } else {
            responseData = ResponseData.ok().putDataValue("status",false).putDataValue("data",null);
        }
        return responseData;
    }


    /**
     * 根据id删除影片信息: 修改status状态
     * @param id 影片id, 从url中动态解析
     * @return ResponseData
     */
    @DeleteMapping("/{id}")
    @ResponseBody
    public ResponseData deleteMovieById(@PathVariable String id) {
        ResponseData responseData = new ResponseData();
        log.info("删除影片,影片ID:{}",id);
        if (null != id && !"".equals(id)) {
            Movie movieFromDB = movieService.getMovieById(Integer.parseInt(id));
            boolean status = Boolean.parseBoolean(movieFromDB.getStatus());
            log.info("status:{}------{}",status,!status);
            Movie m = new Movie();
            m.setId(Integer.parseInt(id));
            m.setStatus(String.valueOf(!status));
            int rs = movieService.updateMovie(m);
            if (rs > 0) {
                responseData = ResponseData.ok().putDataValue("status",true).putDataValue("msg","操作成功");
            } else {
                responseData = ResponseData.error().putDataValue("status",false).putDataValue("msg","操作失败");
            }
        } else {
            responseData = ResponseData.error().putDataValue("status",false).putDataValue("msg","id为空");
        }
        return responseData;
    }

    /**
     * 获取编辑页面, 页面带影片信息
     * @param id 影片id
     * @return mv
     */
    @GetMapping(path = "/edit")
    public ModelAndView getMovieEditPage(@RequestParam("id") String id){
        ModelAndView mv = new ModelAndView();
        Movie movie = movieService.getMovieById(Integer.parseInt(id));
        if (movie!=null){
            mv.clear();
            mv.setViewName("movie/edit_movie");
            mv.addObject("movie",movie);
            mv.addObject("initDate",sdf.format(movie.getDate()));
        }else {
            mv.setViewName("redirect:/page/movie/movie");
        }
        return mv;
    }

    /**
     * 更新影片信息
     * @param movie 影片信息
     * @return mv
     */
    @PostMapping(path = "/edit")
    public ModelAndView editMovie(Movie movie){
        ModelAndView mv = new ModelAndView();
        int rs = movieService.updateMovie(movie);
        if (rs>0){
            mv.setViewName("redirect:/page/movie/movie");
            mv.addObject("msg","数据更新成功");
        }else {
            mv.setViewName("movie/edit_movie");
            mv.addObject("msg","数据更新失败");
        }
        return mv;
    }

    /**
     * 根据id播放影片
     * @param id 影片id
     * @return mv
     */
    @GetMapping("/play")
    public ModelAndView playById(@RequestParam(defaultValue = "1") int id) {
        ModelAndView mv = new ModelAndView();
        Movie movie = movieService.getMovieById(id);
        mv.setViewName("movie/play");
        mv.addObject("mv",movie);
        return mv;
    }

    /**
     * 添加影片信息
     * @param movie 影片信息
     * @return mv
     */
    @PostMapping
    public ModelAndView addUser(Movie movie) {
        log.info("预添加movie的信息：{}",movie);
        ModelAndView mv = new ModelAndView();
        int rs = movieService.saveMovie(movie);
        if (rs>0){
            mv.setViewName("redirect:page/movie/movie");
            mv.addObject("msg","影片:"+movie.getName()+"添加成功");
        }else {
            mv.setViewName("movie/add_movie");
            mv.addObject("msg","影片:"+movie.getName()+"添加失败");
        }
        return mv;
    }
}
